﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace ImageTools
{
    /// <summary>
    /// ImageGenCFileToolUi.xaml 的交互逻辑
    /// </summary>
    public partial class ImageGenCFileToolUi : Window
    {
        public ImageGenCFileToolUi()
        {
            InitializeComponent();
        }

        public void SelectFilesClicked(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog
            {
                Title = "选择文件",
                Multiselect = true,
                Filter = "Image Files (*.bmp, *.jpg, *.jpeg)|*.bmp;*.jpg;*.jpeg"

            };

            if (openFileDialog.ShowDialog() == true)
            {
                string[] selectedFilePath = openFileDialog.FileNames;

                listbox_files.Items.Clear();

                foreach (string filePath in selectedFilePath)
                {
                    listbox_files.Items.Add(filePath);
                }
            }
        }

        public void button_SaveFileClicked(object sender, RoutedEventArgs e)
        {
            var folderDialog = new OpenFolderDialog();
            if (folderDialog.ShowDialog() == true)
            {
                if (folderDialog.ToString() != string.Empty)
                {
                    textBox_outPath.Text = folderDialog.FolderName;
                }
            }
        }

        public void button_GenClicked(object sender, RoutedEventArgs e)
        {
            List<string> fileNames = new List<string>();

            ImageGenCFileTool imageGenCFileTool;

            foreach (string filePath in listbox_files.Items)
            {
                fileNames.Add(filePath);
            }

            if (fileNames.Count == 0)
            {
                return;
            }

            int ditherMode;

            if (checkBox_isDither_rgb565.IsChecked == true)
            {
                ditherMode = 1;
            }
            else
            {
                ditherMode = 0;
            }

            if (textBox_outPath.Text == string.Empty)
            {
                MessageBox.Show("输出目录为空！！");
                return;
            }

            
            // C文件
            imageGenCFileTool = new ImageGenCFileTool(textBox_outPath.Text, findRadioIsChecked(), 
                            ditherMode, comboBox_numAlign.SelectedIndex);

            string[] paths = fileNames.ToArray();

            List<Task> taskList = new List<Task>();

            Task task;

            foreach (string path in paths)
            {
                task = new Task(() =>
                {
                    imageGenCFileTool.ImageCreateCFile(path);
                });

                task.Start();
                taskList.Add(task);
            }

            Task.WaitAll(taskList.ToArray());

            MessageBox.Show("全部转换完成。");
            


        }

        // 获取radioButton被选中
        private uint findRadioIsChecked()
        {
            uint array_type = 0;
            if (radioButton_RGB888.IsChecked == true)
            {
                array_type = 1;
            }
            else if (radioButton_BGR888.IsChecked == true)
            {
                array_type = 2;
            }
            else if (radioButton_RGB565.IsChecked == true)
            {
                array_type = 3;
            }
            else if (radioButton_BGR565.IsChecked == true)
            {
                array_type = 4;
            }
            else if (radioButton_NV12.IsChecked == true)
            {
                array_type = 5;
            }
            else if (radioButton_NV21.IsChecked == true)
            {
                array_type = 6;
            }
            else if (radioButton_NV16.IsChecked == true)
            {
                array_type = 7;
            }
            else if (radioButton_NV61.IsChecked == true)
            {
                array_type = 8;
            }
            else if (radioButton_YUYV.IsChecked == true)
            {
                array_type = 9;
            }
            else if (radioButton_YVYU.IsChecked == true)
            {
                array_type = 10;
            }
            else if (radioButton_UYVY.IsChecked == true)
            {
                array_type = 11;
            }
            else if (radioButton_VYUY.IsChecked == true)
            {
                array_type = 12;
            }

            return array_type;
        }


    }
}
